<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <el-card :style="{'min-height': containerHeight + 'px'}" class="box-card">
            <div slot="header" class="clearfix">
                <el-button type="primary" icon="el-icon-folder-add" @click="goAdd">新增</el-button>
                <el-button type="warning" icon="el-icon-edit" @click="goEdit">修改</el-button>
                <el-button type="danger" icon="el-icon-delete" @click="delAllSelection">删除</el-button>

                <!-- 搜索表单 -->
                <el-form :model="query" ref="searchRuleForm" label-width="100px" style="display: none">
                    <el-row style="margin-top:20px">
                        <el-col :sm="20" :md="5">
                            <el-form-item label="分类类别:" prop="departNo">
                                <el-input
                                    placeholder="请输入内容"
                                    v-model="query.resignationUserName"
                                    size="small"
                                    class="handle-input mr10"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="5">
                            <el-form-item label="分类名称:" prop="departNo">
                                <el-input
                                    placeholder="请输入内容"
                                    v-model="query.resignationUserName"
                                    size="small"
                                    class="handle-input mr10"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="5" style="margin-left:10px">
                            <el-button
                                type="primary"
                                icon="el-icon-search"
                                @click="submitForm('searchRuleForm')"
                            >搜 索</el-button>
                            <el-button type="info" @click="resetForm('searchRuleForm')">重 置</el-button>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <div class="tree-box flex">
                <div class="flex-left">
                    <ul class="" v-if="leftList.length > 0">
                        <li :class="currentIndex == index ? 'current' : ''" v-for="(item, index) in leftList" :key="index" @click="findDictItem(item, index)">{{item.typeName}}</li>
                    </ul>
                    <p v-else class="no-data-p">暂无分类信息</p>
                </div>
                <div class="flex-right">
                    <!-- 表格 -->
                    <table-List
                        :tableData="tableData"
                        :tableHeader="tableHeader"
                        :loading="loading"
                        :pagination="pagination"
                        @pageChange="pageChange"
                        @selectFun="selectFun"
                        @sortChange="sortChange"
                    ></table-List>
                </div>
            </div>
        </el-card>
        <edit-taxonomy 
            :isShow="showEditDialog" 
            :currentItem="currentItem"
            :editData="editData"
            :isEdit="isEdit"
            @submit="editSbmit"
            @close="editClose" 
        />
    </div>
</template>


<script>
import appMain from '../../../utils/app_main';
export default {
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            query: {
                departName: '', //  部门名称
                departNo: '', //部门编号
                resignationUserId: '', //离职人id
                resignationUserName: '', //离职人
                sortColumn: '', //排序字段 倒序追加 columnName desc
                status: '' //流程状态只有查询全部和未提交两种,查询全部就不传值
            },
            loading: false,
            // 分页信息
            pagination: {
                pageIndex: 1,
                pageSize: 10,
                pageTotal: 60
            },
            tableHeader: [
                // 表头数据
                { prop: 'itemName', label: '字典项名称', minWidth: '100px', sortable: 'custom' },
            ],
            tableData: [],
            editData: {}, // 要修改的数据
            isEdit: false,
            multipleSelection: [],

            leftList: [], // 左侧列表
            currentItem: {}, // 当前选择的左侧的项
            currentIndex: '', // 左边选中的index
            showEditDialog: false,
            departList: [] // 部门下拉框数据
        };
    },
    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 160;
        this.pageTitle = this.$router.currentRoute.meta.title;
    },
    methods: {
        goEdit() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
           
            console.log('goEdit', this.multipleSelection);
            this.editData = this.multipleSelection[0];
            this.isEdit = true;
            // this.showEditDialog = true;
            this.$router.push({
                path: '/taxonomyAttribute',
                query: {
                    id: this.currentItem.id,
                    editData:JSON.stringify(this.editData)
                }
            });

        },
        goAdd() {
            // this.editData = {};
            // this.isEdit = false;
            // this.showEditDialog = true;
            this.$router.push({
                path: '/taxonomyAttribute',
                query: {
                    id: this.currentItem.id,
                }
            });
        },
        delAllSelection() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //请求接口删除数据
                    this.deleteById(this.multipleSelection[0].id);
                    this.findAllPage();
                })
                .catch(() => {});
        },
        // 组件选择完后把数据传过来
        selectFun(data) {
            this.multipleSelection = data.backData;
        },
        //表格组件返回排序对象
        sortChange(data) {
            let column = data.backData;
            //排序
            if (column.order) {
                //倒序
                if (column.order === 'descending') {
                    this.query.sortColumn = column.prop + ' ' + 'desc';
                } else {
                    this.query.sortColumn = column.prop;
                }
            } else {
                //不排序
                this.query.sortColumn = '';
            }
            this.findAllPage();
        },
        deleteById(id) {
            appMain
                .dictItemDel({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // 查询左侧列表
        findLeftList () {
            let self = this;
            let params = {
                pageNo: 1,
                pageSize: 1000,
                record: ''
            };
            new Promise((resolve, reject) => {
                appMain
                .dictTypeFindAllPageByTypeName(params)
                    .then(res => {
                        if (res.data.code === 20000) {
                            self.leftList = res.data.data.list;
                            resolve()
                        } else {
                            this.$message.error(res.data.message);
                        }
                    })
                    .catch(err => {
                        console.log(err);
                    });
            }).then (res => {
                if (self.leftList.length > 0) {
                     self.currentItem = self.leftList[0];
                     self.findAllPage()
                }
                
            })
            
        },
        // 查询
        findAllPage () {
            this.loading = true;
            appMain
                .dictItem({typeName: this.currentItem.typeName})
                .then(res => {
                    this.timer = setTimeout(() => {
                        this.loading = false;
                    }, 1000);
                    if (res.data.code === 20000) {
                        //TODO 测试环境使用
                        this.tableData = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                    this.timer = setTimeout(() => {
                        //设置延迟执行
                        this.loading = false;
                    }, 1000);
                });
        },
        findDictItem (item, index) {
            this.currentItem = item;
            this.currentIndex = index;
            this.findAllPage();
        },
        //分页导航
        pageChange(data) {
            this.pagination = data.backData;
            console.log('pageChange:', this.pagination);
            //分页变化--请求接口
            this.findAllPage();
        },
        //树组件搜索结果返回
        searchBack(data) {
            console.log(data.backData, 'searchTreeBack');
            this.query.resignationUserName = data.backData.name;
            this.query.resignationUserId = data.backData.id;
            this.showSearch = false;
        },
        closeTreeDialog(data) {
            this.showSearch = data.showDialog;
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.findAllPage();
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        editSbmit (data) {
            this.findAllPage();
            this.showEditDialog = false
        },
        editClose () {
            this.showEditDialog = false
        }
    },
    created() {
        this.findLeftList();
    }
};
</script>

<style lang="css" scoped>
.el-card {
    height: 100%;
}
.flex-left{
    /* -webkit-box-shadow: 1px 0px 0px #ddd; */
}
.flex-left li{
     box-shadow: 0px 0px 4px 2px #ddd;
     height: 30px;
     line-height: 30px;
     padding-left: 20px;
     margin-bottom: 10px;
     overflow: hidden;
     text-overflow: ellipsis;
}
.flex-left li.current{
    box-shadow: 0px 0px 4px 2px #409EFF;
}

</style>